from random import randint

from django.shortcuts import render

# Create your views here.
from django_redis import get_redis_connection
from rest_framework.response import Response
from rest_framework.views import APIView
from celery_tasks.sms.tasks import send_sms_code

from meiduo_mall.libs.yuntongxun.sms import CCP


class SMSCodeView(APIView):
    def get(self,request,mobile):
        #request.query_pramas 获取查询字符串
        #request.data. #获取请求体数据

        #判断60s
        conn=get_redis_connection('verify')
        flag=conn.get('sms_flag_%s'%mobile)
        if flag:
            return Response({'message':'请求过于频繁'},status=400)

        #生成短信验证码
        sms_code = '%06d'%randint(0,999999)
        print(sms_code)

        #保存短信验证码
        pl=conn.pipeline()
        pl.setex('sms_%s'%mobile,300,sms_code)
        pl.setex('sms_flag_%s'%mobile,60,1)
        pl.execute()   #发送redis执行命令

        #发送短信
        # ccp=CCP()
        # ccp.send_template_sms(mobile,[sms_code,'5'],1)
        send_sms_code.delay(mobile,sms_code)

        #返回结果
        return Response({'message':'ok'})